﻿/********************************************************************************
    Copyright (C) Binod Nepal, Planet Earth Solutions Pvt. Ltd., Kathmandu.
	Released under the terms of the GNU General Public License, GPL, 
	as published by the Free Software Foundation, either version 3 
	of the License, or (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
    See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
***********************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MixNP.Web.Classifieds
{
    public partial class Main : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                short cityId = Pes.Utility.Conversion.TryCastShort(Request["city"]);
                string cityName = string.Empty;
                string userIdentity = string.Empty;

                if (cityId > 0)
                {
                    cityName = MixNP.BusinessLayer.Cities.GetCityName(cityId);

                    if (!string.IsNullOrWhiteSpace(cityName))
                    {
                        Session["CityId"] = cityId.ToString();
                        Session["City"] = cityName;

                        if (Request.IsAuthenticated)
                        {
                            userIdentity = Pes.Utility.Conversion.TryCastString(HttpContext.Current.User.Identity.Name);

                            if (!string.IsNullOrWhiteSpace(userIdentity))
                            {
                                MixNP.BusinessLayer.Users.UpdateCity(userIdentity, cityId);
                            }
                        }
                    }
                }

                Pes.Utility.PageUtility.CheckInvalidAttempts(this.Page);
                string sessionCity = Pes.Utility.Conversion.TryCastString(Session["City"]);

                if (Request.IsAuthenticated)
                {
                    userIdentity = Pes.Utility.Conversion.TryCastString(HttpContext.Current.User.Identity.Name);

                    if (!string.IsNullOrWhiteSpace(userIdentity))
                    {
                        CityLiteral.Text = MixNP.BusinessLayer.Users.GetCity(userIdentity);
                    }
                }
                else
                {
                    if (!string.IsNullOrWhiteSpace(sessionCity))
                    {
                        CityLiteral.Text = sessionCity;
                    }
                }

                if (string.IsNullOrWhiteSpace(CityLiteral.Text))
                {
                    CityLiteral.Text = "Kathmandu";
                }
            }
        }


    }
}